//
//  LoginViewController.m
//  UDuo
//
//  Created by 铅笔 on 15/11/20.
//  Copyright © 2015年 铅笔. All rights reserved.
//

#import "LoginViewController.h"
#import "ResgisterUserVC.h"//注册
#import "ForgetPwdViewController.h"//忘记密码
#import "MailboxBindingVC.h"//邮箱找回密码

#import "myTabBarViewController.h"

#import "AppDelegate.h"

#import "FillInTelPhoneNumberVC.h"

//导入这个就行了
#import <CommonCrypto/CommonDigest.h>

#import "MeViewController.h"

#define CC_MD5_DIGEST_LENGTH    16
#define CC_MD532_DIGEST_LENGTH    32

@interface LoginViewController ()<UITextFieldDelegate,UIActionSheetDelegate,HeadBackView>
{
    UIButton *button_getCheckCode;//获取验证码
    
    UIView *view_cover;//这盖面
    UIView *view_popBG;//弹出视图
    int starTime;
    
    dispatch_source_t _timer;
    
}

@property (nonatomic,strong) UIImageView *imageView_logo;
@property (nonatomic,strong) UIView *view_setLoginNameBG;//设置登录名背景
@property (nonatomic,strong) UIView *view_buttomBG;//底部注册 忘记密码背景
@property (nonatomic,strong) UIView *view_passWordBG;//设置登录密码背景

@property (nonatomic,strong) UITextField *textField_loginName;
@property (nonatomic,strong) UITextField *textField_password;
@property (nonatomic,strong) UIImageView *imageView_loginName;
@property (nonatomic,strong) UIImageView *imageView_password;
@property (nonatomic,strong) UIButton *button_login;

@property (nonatomic,strong) UIButton *button_checkCode;
@property (nonatomic,strong) UIButton *button_forgetPwd;
@property (nonatomic,strong) UIButton *button_register;
@property (nonatomic,strong) UIButton *button_backHome;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RXDColor(237, 243, 243);
    
    HeadBackView *headbackview=[HeadBackView headbackView:@"登录" andrightBtn:nil];
    [self.view addSubview:headbackview];
    headbackview.delegate=self;
    
    [self initWithAllSubviews];
    
    [self initWithButtomView];

    //注册键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark ---返回上一个界面
- (void) backUpToForwardView
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 按钮事件
-(void)btnback
{
    if ([self.fromeWhere isEqualToString:@"chooseCredit"] || [self.fromeWhere isEqualToString:@"detail"]) {
        [self.tabBarController setSelectedIndex:2];
        [self.navigationController popViewControllerAnimated:YES];
    }
    if ([self.fromeWhere isEqualToString:@"appdelegate"])
    {
        myTabBarViewController *tabBarView = [[myTabBarViewController alloc] init];
        self.view.window.rootViewController = tabBarView;
    }
    else
    {
        [self.tabBarController setSelectedIndex:0];
        [self.navigationController popToRootViewControllerAnimated:YES];
        self.tabBarController.tabBar.hidden=NO;
    }
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
     self.tabBarController.tabBar.hidden = YES;
     self.navigationController.navigationBarHidden = YES;
}

#pragma mark ---- 创建子视图
- (void) initWithAllSubviews
{
   //logo
    self.imageView_logo = [[UIImageView alloc] initWithFrame:CGRectMake(290*PROPORTION_WIDTH,90*PROPORTION_WIDTH+64,145*PROPORTION_WIDTH, 145*PROPORTION_WIDTH)];
    [self.view addSubview:self.imageView_logo];
    self.imageView_logo.image = [UIImage imageNamed:@"ic_mrbang"];
    
    //设置登录密码背景
    self.view_setLoginNameBG = [[UIView alloc] initWithFrame:CGRectMake(20,self.imageView_logo.bottom+50*PROPORTION_WIDTH,kScreenWidth - 40, 70*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_setLoginNameBG];
    self.view_setLoginNameBG.backgroundColor = RXDColor(253, 253, 253);
    
    self.view_setLoginNameBG.layer.cornerRadius = 3.0;
    self.view_setLoginNameBG.clipsToBounds = YES;
    
    self.textField_loginName = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,kScreenWidth - 100, self.view_setLoginNameBG.height)];
    [self.view_setLoginNameBG addSubview:self.textField_loginName];
    self.textField_loginName.tag = 1100;
    self.textField_loginName.placeholder = @" 输入手机号";
    self.textField_loginName.font = [UIFont systemFontOfSize:Font_SIZE_30];
    self.textField_loginName.keyboardType = UIKeyboardTypeNumberPad;
    [self.textField_loginName addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.imageView_loginName = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 70, 10,25, 25)];
    [self.view_setLoginNameBG addSubview:self.imageView_loginName];
    self.imageView_loginName.image = [UIImage imageNamed:@"login_user"];
    
    //设置登录密码背景
    self.view_passWordBG = [[UIView alloc] initWithFrame:CGRectMake(20, self.view_setLoginNameBG.bottom+45*PROPORTION_WIDTH,kScreenWidth - 40, 70*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_passWordBG];
    self.view_passWordBG.backgroundColor = RXDColor(253, 253, 253);
    self.view_passWordBG.layer.cornerRadius = 3.0;
    self.view_passWordBG.clipsToBounds = YES;
    
    self.textField_password = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,kScreenWidth - 100, self.view_passWordBG.height)];
    [self.view_passWordBG addSubview:self.textField_password];
    self.textField_password.tag = 1101;
    self.textField_password.placeholder = @" 输入密码";
    self.textField_password.secureTextEntry = YES;
    self.textField_password.font = [UIFont systemFontOfSize:Font_SIZE_30];
    [self.textField_password addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.imageView_password = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 70, 10,25, 25)];
    [self.view_passWordBG addSubview:self.imageView_password];
    self.imageView_password.image = [UIImage imageNamed:@"login_pwd"];
    
    //验证码
    [self initWithCheckCodeLogin];
    
    //设置修改button
    self.button_login = [[UIButton alloc] initWithFrame:CGRectMake(20, self.view_passWordBG.bottom+100*PROPORTION_WIDTH,kScreenWidth - 40, 92*PROPORTION_WIDTH)];
    [self.view addSubview:self.button_login];
    self.button_login.backgroundColor = COLOR_UNBUTTON;
    [self.button_login setTitle:@"确  定" forState:UIControlStateNormal];
    self.button_login.layer.cornerRadius = 3.0;
    self.button_login.clipsToBounds = YES;
    self.button_login.tag = 10;
    [self.button_login.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
    
    [self.button_login addTarget:self action:@selector(loginPwdRegisterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

/**
 *键盘是否亮起
 */
- (void)textFieldDidChange:(UITextField *)textField
{
    switch (textField.tag) {
        case 1100:
            if (textField==self.textField_loginName) {
                if (self.textField_loginName.text.length > 10 ) {
                    textField.text = [textField.text substringToIndex:11];
                    if ( self.textField_password.text.length>5) {
                        self.button_login.backgroundColor = COLOR_BUTTON;
                        self.button_login.userInteractionEnabled=YES;
                        
                    }
                }
                else
                {
                    self.button_login.backgroundColor = COLOR_UNBUTTON;
                    self.button_login.userInteractionEnabled = NO;
                }
            }
            break;
        case 1101:
            
            if ([textField.placeholder isEqualToString:@" 输入验证密码"] && textField.text.length>5) {
                
                textField.text = [textField.text substringToIndex:6];
                self.button_login.backgroundColor=COLOR_BUTTON;
                self.button_login.userInteractionEnabled=YES;
            }
            else
            {
                if (textField.text.length>5 &&self.textField_loginName.text.length==11)
                {
                    
                    self.button_login.backgroundColor=COLOR_BUTTON;
                    self.button_login.userInteractionEnabled=YES;
                    
                }
                else
                {
                    self.button_login.backgroundColor = COLOR_UNBUTTON;
                    self.button_login.userInteractionEnabled=NO;
                }
            }
            
            break;
        default:
            break;
    }
}

#pragma mark --- 底部视图
- (void) initWithButtomView
{
    self.view_buttomBG = [[UIView alloc] initWithFrame:CGRectMake(0,kScreenHeight-90*PROPORTION_WIDTH,kScreenWidth, 90*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_buttomBG];
    self.view_buttomBG.backgroundColor = RXDColor(50, 68, 78);
    
    //忘记密码
    self.button_forgetPwd = [[UIButton alloc] initWithFrame:CGRectMake(20,0,(kScreenWidth - 40)/2, self.view_buttomBG.height)];
    [self.view_buttomBG addSubview:self.button_forgetPwd];
    [self.button_forgetPwd setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [self.button_forgetPwd.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
    
    [self.button_forgetPwd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.button_forgetPwd.tag = 11;
    
    [self.button_forgetPwd addTarget:self action:@selector(loginPwdRegisterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //注册
    self.button_register = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth - 40)/2+20,0,(kScreenWidth - 40)/2,self.view_buttomBG.height)];
    [self.view_buttomBG addSubview:self.button_register];
    [self.button_register setTitle:@"注册" forState:UIControlStateNormal];
    [self.button_register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.button_register.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
    self.button_register.tag = 12;
    
    [self.button_register addTarget:self action:@selector(loginPwdRegisterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark --- 验证码登录
- (void) initWithCheckCodeLogin
{
    //忘记密码
    self.button_checkCode = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-20-75,self.view_passWordBG.frame.size.height+self.view_passWordBG.frame.origin.y+5,75, 30)];
    [self.view addSubview:self.button_checkCode];
    [self.button_checkCode setTitle:@"验证码登录" forState:UIControlStateNormal];
    [self.button_checkCode setTitle:@"密码登录" forState:UIControlStateSelected];

    [self.button_checkCode setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.button_checkCode.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_24]];
    self.button_checkCode.selected = NO;
    
    [self.button_checkCode addTarget:self action:@selector(checkCodeLoginAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark ---- 验证码登录
- (void) checkCodeLoginAction:(UIButton *)sender
{
    //将键盘退出
    [self.textField_password resignFirstResponder];
    
    self.view_passWordBG = [[UIView alloc] initWithFrame:CGRectMake(20, self.view_setLoginNameBG.bottom+45*PROPORTION_WIDTH,kScreenWidth - 40, 70*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_passWordBG];
    self.view_passWordBG.backgroundColor = RXDColor(253, 253, 253);
    self.view_passWordBG.layer.cornerRadius = 3.0;
    self.view_passWordBG.clipsToBounds = YES;
    
    self.textField_password = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,kScreenWidth - 100, self.textField_password.height)];
    [self.view_passWordBG addSubview:self.textField_password];
   
    button_getCheckCode = [[UIButton alloc] init];
    button_getCheckCode.backgroundColor = RXDColor(255, 118, 112);
    [button_getCheckCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    [button_getCheckCode.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_24]];
    button_getCheckCode.layer.cornerRadius = 3.0;
    button_getCheckCode.clipsToBounds = YES;
    self.textField_password.secureTextEntry = YES;
    self.textField_password.font = [UIFont systemFontOfSize:Font_SIZE_30];
    
    [self.textField_password addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.textField_password.tag = 1101;
    
    if (sender.selected==YES) {
        sender.selected=NO;
        
        //终止验证码发送
        if (starTime<60&&starTime>0) {
            dispatch_source_cancel(_timer);
        }
        self.textField_password.placeholder = @" 输入密码";
    }
    else
    {
        sender.selected=YES;
        self.textField_password.secureTextEntry = NO;
        self.textField_password.placeholder = @" 输入验证密码";
        self.textField_password.keyboardType = UIKeyboardTypeNumberPad;
        
        button_getCheckCode.frame = CGRectMake(self.view_passWordBG.frame.size.width-90,1, 90, self.textField_password.height-2);
        [self.view_passWordBG addSubview:button_getCheckCode];
        [button_getCheckCode addTarget:self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark --- 获取验证密码
- (void) getCheckPedAction
{
    if (self.textField_loginName.text.length!=11) {
        [BaseViewController showMessage:@"请填写正确手机号"];
        return;
    }
    else
    {
        NSDictionary *paras_checkCode = @{@"mobile":self.textField_loginName.text,@"type":[NSNumber numberWithInt:1]};
        
        NSString *strURL = [NSString stringWithFormat:@"%@captcha",MRAPI];
        
        [[HttpRequest shardWebUtil] postNetworkRequestURLString:strURL parameters:paras_checkCode success:^(id obj) {
            
            NSLog(@"obj----%@",obj);
            NSString *str_false = obj[@"error"];
            
            if (str_false.length==0)
            {
                
            }
            else
            {
                [BaseViewController showMessage:str_false];
            }
        } fail:^(NSError *error) {
            
        }];
    }
}

#pragma mark - 倒计时

- (void)startTime
{
    if (self.textField_loginName.text.length==0) {
        [UIView showAlertMsg:@"请输入手机号"];
        return;
    }
    if (![BaseViewController isMobileNumber_str:self.textField_loginName.text]) {
        [UIView showAlertMsg:@"请输入正确手机号"];
        return;
    }
    
    [self getCheckPedAction];
    
    starTime=60;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(starTime<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [button_getCheckCode setTitle:@"重新发送" forState:UIControlStateNormal];
                button_getCheckCode.userInteractionEnabled = YES;
                button_getCheckCode.backgroundColor = COLOR_BUTTON;
            });
        }else{
           
            NSString *strTime = [NSString stringWithFormat:@"%.2d", starTime];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [button_getCheckCode setTitle:[NSString stringWithFormat:@"(%@)秒后\n重新发送",strTime] forState:UIControlStateNormal];
                button_getCheckCode.backgroundColor = COLOR_UNBUTTON;
                button_getCheckCode.titleLabel.numberOfLines=2;
                button_getCheckCode.userInteractionEnabled = NO;
                
            });
            starTime--;
        }
    });
    dispatch_resume(_timer);
}

#pragma mark ---- 登录 忘记密码 注册
- (void) loginPwdRegisterButtonAction:(UIButton *)difButton
{
    if (difButton.tag == 10) {
        
        if (self.textField_loginName.text.length==0) {
            [BaseViewController showMessage:@"请填写账号"];
        }
       
        if (self.textField_password.text==0) {
            [BaseViewController showMessage:@"请填写密码"];
        }
        else
        {
            [self.textField_loginName resignFirstResponder];
            [self.textField_password resignFirstResponder];
            
            //密码登录
            NSDictionary *paras_register;
            if (self.button_checkCode.selected==NO) {
              paras_register = @{@"mobile":self.textField_loginName.text,@"password":self.textField_password.text};
            }
            else
            {
                paras_register = @{@"mobile":self.textField_loginName.text,@"captcha":self.textField_password.text};
            }
            NSString *strUrl = [NSString stringWithFormat:@"%@member/login",MRAPI];
            
            [[HttpRequest shardWebUtil] postNetworkRequestURLString:strUrl parameters:paras_register success:^(id obj) {
                
                NSLog(@"login%@",obj);
                
                NSString *str_false = obj[@"error"];
                if (str_false.length==0) {
                    [BaseViewController showMessage:@"登录成功!"];
                    
                    [[NSUserDefaults standardUserDefaults] setObject:obj[@"memberid"] forKey:@"memberid"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    
                    [[NSUserDefaults standardUserDefaults] setObject:obj[@"mobile"] forKey:@"mobile"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    
                    [[NSUserDefaults standardUserDefaults] setObject:obj[@"issetpaypassword"] forKey:@"issetpaypassword"];
                    [[NSUserDefaults standardUserDefaults] synchronize];

                    if ([self.fromeWhere isEqualToString:@"chooseCredit"] || [self.fromeWhere isEqualToString:@"detail"] || [self.fromeWhere isEqualToString:@"bagApplyNow"])
                    {
                        [self.tabBarController setSelectedIndex:0];
                        [self.navigationController popViewControllerAnimated:YES];
                    }
                    if ([self.fromeWhere isEqualToString:@"appdelegate"])
                    {
                        //添加一个是否进行 忘记密码重置标记
                        [[NSUserDefaults standardUserDefaults] setObject:@"3" forKey:@"indexTabbar"];
                        [[NSUserDefaults standardUserDefaults] synchronize];

                        myTabBarViewController *tabBarView = [[myTabBarViewController alloc] init];
                        self.view.window.rootViewController = tabBarView;
                    }
                   if ([self.fromeWhere isEqualToString:@"meCenter"])
                     {
                      [self.navigationController popToRootViewControllerAnimated:YES];
                       
                    }
                    else
                    {
                        [self.navigationController popViewControllerAnimated:YES];
                    }
                }
                else
                {
                    [BaseViewController showMessage:str_false];
                }
                
            } fail:^(NSError *error) {
                NSLog(@"error%@",error);
            }];
        }
    }
    if (difButton.tag == 11) {
        //忘记密码
        [self choseAvatarFromLibraryAction];
        
    }
    if (difButton.tag == 12) {
       
        //注册
        ResgisterUserVC *registrView = [[ResgisterUserVC alloc] init];
        [self.navigationController pushViewController:registrView animated:YES];
    }
}

#pragma mark --- 忘记密码？
- (void) choseAvatarFromLibraryAction
{
    view_cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:view_cover];
    view_cover.backgroundColor = [UIColor blackColor];
    view_cover.alpha = 0.5;
    
    //添加手势
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle)];
    [view_cover addGestureRecognizer:gesture];
    
    view_popBG = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight-300, kScreenWidth, 300)];
    [self.view addSubview:view_popBG];
    view_popBG.backgroundColor = [UIColor whiteColor];
    
    [self forgetPasswordFindPopView];
}

#pragma mark - popView 弹出的视图
- (void) forgetPasswordFindPopView
{
    NSArray *array_title = @[@"通过手机号码找回",@"通过绑定邮箱找回",@"联系客服"];
    for (int i=0; i<4; i++) {
        CGFloat buttonY = (40+20)*i;
        UIButton *button_action = [[UIButton alloc] initWithFrame:CGRectMake(20, 20 + buttonY, kScreenWidth-40, 50)];
        [view_popBG addSubview:button_action];
        
        button_action.tag = 20+i;
        
        button_action.layer.cornerRadius = 3.0;
        button_action.clipsToBounds = YES;
        
        if (i==3) {
            button_action.backgroundColor = RXDColor(179, 179, 179);
            
            [button_action setTitle:@"取消" forState:UIControlStateNormal];
        }
        else
        {
            button_action.layer.borderColor = RXDColor(233, 233, 233).CGColor;
            button_action.layer.borderWidth = 1.0;
            [button_action setTitle:array_title[i] forState:UIControlStateNormal];
            [button_action setTitleColor:RXDColor(110, 110, 110) forState:UIControlStateNormal];
        
        }
        [button_action addTarget:self action:@selector(findPasswordStyle:) forControlEvents:UIControlEventTouchUpInside];
    }
}

//找回密码
- (void) findPasswordStyle:(UIButton *)sender
{
    if (sender.tag==20) {
        ForgetPwdViewController *forgetPws = [[ForgetPwdViewController alloc] init];
        [self.navigationController pushViewController:forgetPws animated:YES];
    }
    if (sender.tag==21) {
        MailboxBindingVC *mailboxView = [[MailboxBindingVC alloc] init];
        mailboxView.str_title = @"邮箱找回密码";
        [self.navigationController pushViewController:mailboxView animated:YES];
    }
    if (sender.tag==22) {
        
    }
    if (sender.tag==23) {
        
    }
    
    [view_popBG removeFromSuperview];
    [view_cover removeFromSuperview];
}

//手势移除
- (void) tapHandle
{

    [view_popBG removeFromSuperview];
    [view_cover removeFromSuperview];
}

//md5, 32位加密
//- (NSString*)md5lowercase32:(NSString *)string{
//    
//    const char* str = [string UTF8String];
//    unsigned char result[CC_MD5_DIGEST_LENGTH];
//    CC_MD5(str, (unsigned int)strlen(str), result);
//    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
//    
//    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
//        [ret appendFormat:@"%02x",result[i]];
//    }
//    return ret;
//}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark keyboardNotification

- (void)keyboardWillShow:(NSNotification*)notification{
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //如果self在键盘之下 才做偏移
    if ([self convertYToWindow:CGRectGetMaxY(self.button_login.frame)]>=_keyboardRect.origin.y)
    {
        //没有偏移 就说明键盘没出来，使用动画
        if (self.view.frame.origin.y== self.button_login.frame.origin.y) {
            
            [UIView animateWithDuration:0.3 animations:^{
                self.view.frame = CGRectMake(0, -_keyboardRect.size.height+[self getHeighOfWindow]-CGRectGetMaxY(self.view.frame), kScreenWidth, kScreenHeight);
            }];
        }
        else
        {
            self.view.frame = CGRectMake(0, -105, kScreenWidth, kScreenHeight);
        }
    }
}

- (void)keyboardWillHide:(NSNotification*)notification{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }];
}

#pragma  mark ConvertPoint
//将坐标点y 在window和superview转化  方便和键盘的坐标比对
-(float)convertYFromWindow:(float)Y
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CGPoint o = [appDelegate.window convertPoint:CGPointMake(0, Y) toView:self.view];
    return o.y;
    
}
-(float)convertYToWindow:(float)Y
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CGPoint o = [self.view convertPoint:CGPointMake(0, Y) toView:appDelegate.window];
    return o.y;
    
}
-(float)getHeighOfWindow
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    return appDelegate.window.frame.size.height;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
